package com.yingpai.fitness.dvp_2_0.main.fragment.mine.assmble;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.yingpai.fitness.R;
import com.yingpai.fitness.base.BaseResponse;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.GsonUtil;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XListView;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.LogisticsActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.assmble.AssmbleCommentActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.assmble.AssmbleRefundServiceActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.assmble.AssmbleOrderActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.assmble.AssmbleShopDetailActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.CommentListBean;
import com.yingpai.fitness.dvp_2_0.main.bean.assmble.AssmbleDetailBean;
import com.yingpai.fitness.dvp_2_0.main.bean.assmble.AssmbleOrderListBean;
import com.yingpai.fitness.dvp_2_0.main.bean.pay.ChongZhiDingDanEntiy;
import com.yingpai.fitness.dvp_2_0.main.bean.pay.ZfbChongZhiEntity;
import com.yingpai.fitness.dvp_2_0.main.callback.WechatCallBack;
import com.yingpai.fitness.dvp_2_0.main.util.DialogUtil;
import com.yingpai.fitness.dvp_2_0.main.util.PayUtil;
import com.yingpai.fitness.dvp_2_0.main.util.ToastUtil;
import com.yingpai.fitness.dvp_2_0.main.util.WechatLoginUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.round.RCRelativeLayout;
import com.yingpai.fitness.entity.ShopDetailMcipChildBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.iwgang.countdownview.CountdownView;

/**
 * Created by ASUS on 2018/11/23.
 */

public class AssmbeAllOrderFragment extends CommonFragment implements OnRefreshLoadMoreListener, PayUtil.codeResultClickInterface, WechatCallBack {

    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    LinearLayout empty;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;
    Unbinder unbinder;
    private String status;
    private QuickAdapter<AssmbleOrderListBean.DataBean.PageInfoBean.ListBeanX> quickAdapter;
    private TokenBean tokenBean;
    private int pageNum = 1;
    private int pageSize = 10;
    private String tag = "";
    private String orderId;
//    private DecimalFormat df = new DecimalFormat("######.###");

    public static AssmbeAllOrderFragment newInstance(String status) {
        AssmbeAllOrderFragment orderListFragment = new AssmbeAllOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        orderListFragment.setArguments(bundle);
        return orderListFragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_0_fragment_order_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        title_bar.setVisibility(View.GONE);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));
        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        status = getArguments().getString("status");

        quickAdapter = new QuickAdapter<AssmbleOrderListBean.DataBean.PageInfoBean.ListBeanX>(context, R.layout.item_order_list) {
            @Override
            protected void convert(final BaseAdapterHelper helper, final AssmbleOrderListBean.DataBean.PageInfoBean.ListBeanX pItem) {
                helper.setText(R.id.title, "订单号：" + pItem.getOrderCode());
                if (pItem.getDetailList() != null && pItem.getDetailList().size() != 0 && pItem.getDetailList().get(0).getList() != null && pItem.getDetailList().get(0).getList().size() != 0) {
                    helper.setText(R.id.shop_count, "共" + pItem.getDetailList().get(0).getList().get(0).getPurchaseAmount() + "件商品");
                }
                helper.setText(R.id.money, "￥" + pItem.getTotalPrice());
                if (pItem.getDetailList() != null && pItem.getDetailList().size() != 0) {
                    helper.setText(R.id.order, pItem.getDetailList().get(0).getBrandName());
                }
                TextView right_tv = helper.getView(R.id.right_tv);
                TextView left_tv = helper.getView(R.id.left_tv);
                TextView center_tv = helper.getView(R.id.center_tv);
                TextView assmble_status = helper.getView(R.id.assmble_status);
                LinearLayout timeLl = helper.getView(R.id.time_ll);
                final CountdownView time = helper.getView(R.id.time);

                if (pItem.getGroupStatus().equals("30")) {
                    assmble_status.setText("拼团中");
                    right_tv.setVisibility(View.VISIBLE);
                    left_tv.setVisibility(View.GONE);
                    center_tv.setVisibility(View.GONE);
                    timeLl.setVisibility(View.GONE);
                    right_tv.setText("查看详情");
                    right_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showDialog(AssmbeAllOrderFragment.this.context, null);
                            HashMap<String, Object> httpParams = new HashMap();
                            httpParams.put("orderId", pItem.getOrderId());
                            finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_DETAIL_REFRESH, httpParams, Url.ASSMBLE_DETAIL_REFRESH, new Callback<AssmbleDetailBean>() {
                                @Override
                                public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                                    dismissDialog();
                                    if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                                        Bundle bundle = new Bundle();
                                        bundle.putSerializable("assmbleDetailBean", assmbleDetailBean);
                                        startActivity(AssmbleOrderActivity.class, bundle);
                                    } else {
                                        showToast(assmbleDetailBean.getMessage() + "");
                                    }
                                }

                                @Override
                                public void onError(Response response) {
                                    super.onError(response);
                                    dismissDialog();
                                }

                            });
                        }
                    });
                } else if (pItem.getGroupStatus().equals("31")) {
                    assmble_status.setText("拼团中,待支付");
                    right_tv.setVisibility(View.VISIBLE);
                    left_tv.setVisibility(View.VISIBLE);
                    timeLl.setVisibility(View.VISIBLE);
                    center_tv.setVisibility(View.GONE);
                    if (pItem.getCountDown() != null) {
                        time.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                            @Override
                            public void onViewAttachedToWindow(View v) {
                                time.start(Long.parseLong(pItem.getCountDown()) - System.currentTimeMillis());
                            }

                            @Override
                            public void onViewDetachedFromWindow(View v) {
                                time.stop();
                            }
                        });
                        time.setOnCountdownEndListener(new CountdownView.OnCountdownEndListener() {
                            @Override
                            public void onEnd(CountdownView countdownView) {
                                onRefresh(refreshLayout);
                            }
                        });
                    }
                    right_tv.setText("立即付款");
                    left_tv.setText("取消订单");
                    right_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogUtil.getInstance().showBottomDialog(AssmbeAllOrderFragment.this.context, R.layout.bottom_pay_layout);
                            View dialog_layout = DialogUtil.getInstance().dialog_bottom_layout;
                            dialog_layout.findViewById(R.id.outImg).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_bottom.dismiss();
                                }
                            });

                            TextView payPriceTv = dialog_layout.findViewById(R.id.payPriceTv);
                            payPriceTv.setText("支付 ￥" + (Double.parseDouble(pItem.getTotalPrice()) + Double.parseDouble(pItem.getTotalPostPrice())));

                            final CheckBox mWeChatPayCb = dialog_layout.findViewById(R.id.weChatPayCb);
                            final CheckBox mALiPayCb = dialog_layout.findViewById(R.id.aLiPayCb);

                            mWeChatPayCb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mWeChatPayCb.isChecked()) {
                                        tag = Constant.WECHAT;
                                        mALiPayCb.setChecked(false);
                                    } else {
                                        tag = "";
                                    }
                                }
                            });
                            mALiPayCb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mALiPayCb.isChecked()) {
                                        tag = Constant.ALIPAY;
                                        mWeChatPayCb.setChecked(false);
                                    } else {
                                        tag = "";
                                    }
                                }
                            });

                            Button goPayBtn = dialog_layout.findViewById(R.id.goPayBtn);

                            goPayBtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (tag.equals(Constant.ALIPAY)) {
                                        orderId = pItem.getOrderId();
                                        showDialog(AssmbeAllOrderFragment.this.context, "支付中...");
                                        HashMap<String, Object> httpParams = new HashMap();
                                        httpParams.put("orderId", pItem.getOrderId());
                                        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_SUER_OEDER_ALIPAY, httpParams, Url.ASSMBLE_SUER_OEDER_ALIPAY, new Callback<ZfbChongZhiEntity>() {
                                            @Override
                                            public void onSuccess(ZfbChongZhiEntity zfbChongZhiEntity) {
                                                if (zfbChongZhiEntity.getResult().equals(Constant.REQUEST_SUCCESS)) {
                                                    PayUtil.zfbpay(AssmbeAllOrderFragment.this, AssmbeAllOrderFragment.this.context, zfbChongZhiEntity.getMap().getResponse());
                                                } else {
                                                    showToast(zfbChongZhiEntity.getMsg() + "");
                                                }
                                                DialogUtil.getInstance().dialog_bottom.dismiss();
                                            }

                                            @Override
                                            public void onError(Response<String> response) {
                                                super.onError(response);
                                                dismissDialog();
                                            }
                                        });
                                    } else if (tag.equals(Constant.WECHAT)) {
                                        orderId = pItem.getOrderId();
                                        showDialog(AssmbeAllOrderFragment.this.context, "支付中...");
                                        HashMap<String, Object> httpParams = new HashMap();
                                        httpParams.put("orderId", pItem.getOrderId());
                                        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_SUER_ORDER_WECHAT, httpParams, Url.ASSMBLE_SUER_ORDER_WECHAT, new Callback<ChongZhiDingDanEntiy>() {
                                            @Override
                                            public void onSuccess(ChongZhiDingDanEntiy chongZhiDingDanEntiy) {
                                                if (chongZhiDingDanEntiy.getResult().equals(Constant.REQUEST_SUCCESS)) {
                                                    WechatLoginUtil.getInstance().setWechatCallBack(AssmbeAllOrderFragment.this);
                                                    PayUtil.wxpay(AssmbeAllOrderFragment.this.context, chongZhiDingDanEntiy.getMap().getResponse().getAppid(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getPartnerid(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getPrepayid(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getNoncestr(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getTimestamp() + "",
                                                            chongZhiDingDanEntiy.getMap().getResponse().getSign());
                                                } else {
                                                    showToast(chongZhiDingDanEntiy.getMsg() + "");
                                                }
                                                DialogUtil.getInstance().dialog_bottom.dismiss();
                                            }

                                            @Override
                                            public void onError(Response<String> response) {
                                                super.onError(response);
                                                dismissDialog();
                                            }
                                        });
                                    } else {
                                        showToast("请选择支付方式");
                                    }

                                }
                            });
                        }
                    });
                    left_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogUtil.getInstance().showCenterDialog(AssmbeAllOrderFragment.this.context, R.layout.dialog_shop_pay_success);
                            View dialog_layout = DialogUtil.getInstance().dialog_center_layout;

                            TextView title = dialog_layout.findViewById(R.id.title);
                            title.setText("确定取消订单？");
                            TextView message = dialog_layout.findViewById(R.id.message);
                            message.setVisibility(View.GONE);
                            dialog_layout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);

                            dialog_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                }
                            });

                            dialog_layout.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                    showDialog(AssmbeAllOrderFragment.this.context, null);
                                    HashMap<String, Object> httpParams = new HashMap();
                                    httpParams.put("orderId", pItem.getOrderId());
                                    finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_CANCEL_ORDER, httpParams, Url.ASSMBLE_CANCEL_ORDER, new Callback<BaseResponse>() {
                                        @Override
                                        public void onSuccess(BaseResponse baseResponse) {
                                            dismissDialog();
                                            if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
                                                onRefresh(refreshLayout);
                                            } else {
                                                showToast(baseResponse.getMsg());
                                            }
                                        }

                                        @Override
                                        public void onError(Response<String> response) {
                                            super.onError(response);
                                            dismissDialog();
                                        }
                                    });
                                }
                            });
                            dialog_layout.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                }
                            });
                        }
                    });
                } else if (pItem.getGroupStatus().equals("20")) {
                    assmble_status.setText("拼团成功");
                    if (pItem.getStatus() == 1 ) {
                        right_tv.setText("查看详情");
                        center_tv.setText("提醒发货");
                        right_tv.setVisibility(View.VISIBLE);
                        center_tv.setVisibility(View.VISIBLE);
                        left_tv.setVisibility(View.GONE);
                        timeLl.setVisibility(View.GONE);
                        center_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showToast("提醒成功");
                            }
                        });
                        right_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showDialog(AssmbeAllOrderFragment.this.context, null);
                                HashMap<String, Object> httpParams = new HashMap();
                                httpParams.put("orderId", pItem.getOrderId());
                                finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_DETAIL_REFRESH, httpParams, Url.ASSMBLE_DETAIL_REFRESH, new Callback<AssmbleDetailBean>() {
                                    @Override
                                    public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                                        dismissDialog();
                                        if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                                            Bundle bundle = new Bundle();
                                            bundle.putSerializable("assmbleDetailBean", assmbleDetailBean);
                                            startActivity(AssmbleOrderActivity.class, bundle);
                                        } else {
                                            showToast(assmbleDetailBean.getMessage() + "");
                                        }
                                    }

                                    @Override
                                    public void onError(Response response) {
                                        super.onError(response);
                                        dismissDialog();
                                    }

                                });
                            }
                        });
                    } else if (pItem.getStatus() == 2) {
                        right_tv.setText("查看详情");
                        center_tv.setText("确认收货");
                        left_tv.setText("查看物流");
                        right_tv.setVisibility(View.VISIBLE);
                        left_tv.setVisibility(View.VISIBLE);
                        center_tv.setVisibility(View.VISIBLE);
                        timeLl.setVisibility(View.GONE);
                        center_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //确认收货
                                showDialog(AssmbeAllOrderFragment.this.context, null);
                                HashMap<String, Object> httpParams = new HashMap();
                                httpParams.put("id", pItem.getOrderId());
                                finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                                        Contacts.HOST_SERVER + Url.ASSMBLE_SURE_RECEIVE,
                                        httpParams, Url.ASSMBLE_SURE_RECEIVE, new Callback<CommentListBean>() {
                                            @Override
                                            public void onSuccess(CommentListBean bean) {
                                                dismissDialog();
                                                if(bean.getResult().equals(Constant.REQUEST_SUCCESS)){
                                                    onRefresh(refreshLayout);
                                                }else{
                                                    ToastUtil.showToast(bean.getMsg());
                                                }
                                            }

                                            @Override
                                            public void onError(Response<String> response) {
                                                super.onError(response);
                                                dismissDialog();
                                            }
                                        });
                            }
                        });
                        left_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Bundle bundle = new Bundle();
                                bundle.putString("com", pItem.getExpressCompanyCode() + "");
                                bundle.putString("num", pItem.getExpressNumber() + "");
                                bundle.putString("state", pItem.getStatus() + "");
                                startActivity(LogisticsActivity.class, bundle);
                            }
                        });
                        right_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showDialog(AssmbeAllOrderFragment.this.context, null);
                                HashMap<String, Object> httpParams = new HashMap();
                                httpParams.put("orderId", pItem.getOrderId());
                                finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                                        Contacts.HOST_SERVER + Url.ASSMBLE_DETAIL_REFRESH,
                                        httpParams, Url.ASSMBLE_DETAIL_REFRESH, new Callback<AssmbleDetailBean>() {
                                            @Override
                                            public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                                                dismissDialog();
                                                if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                                                    Bundle bundle = new Bundle();
                                                    bundle.putSerializable("assmbleDetailBean", assmbleDetailBean);
                                                    startActivity(AssmbleOrderActivity.class, bundle);
                                                } else {
                                                    showToast(assmbleDetailBean.getMessage() + "");
                                                }
                                            }

                                            @Override
                                            public void onError(Response response) {
                                                super.onError(response);
                                                dismissDialog();
                                            }

                                        });
                            }
                        });
                    } else if (pItem.getStatus() == 3) {
                        right_tv.setText("查看详情");
                        center_tv.setText("评价");
                        right_tv.setVisibility(View.VISIBLE);
                        center_tv.setVisibility(View.VISIBLE);
                        left_tv.setVisibility(View.GONE);
                        timeLl.setVisibility(View.GONE);

                        center_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("listBean", pItem);
                                bundle.putString("flag","spell");
                                startActivity(AssmbleCommentActivity.class, bundle);
                            }
                        });
                        right_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showDialog(AssmbeAllOrderFragment.this.context, null);
                                HashMap<String, Object> httpParams = new HashMap();
                                httpParams.put("orderId", pItem.getOrderId());
                                finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                                        Contacts.HOST_SERVER + Url.ASSMBLE_DETAIL_REFRESH,
                                        httpParams, Url.ASSMBLE_DETAIL_REFRESH, new Callback<AssmbleDetailBean>() {
                                            @Override
                                            public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                                                dismissDialog();
                                                if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                                                    Bundle bundle = new Bundle();
                                                    bundle.putSerializable("assmbleDetailBean", assmbleDetailBean);
                                                    startActivity(AssmbleOrderActivity.class, bundle);
                                                } else {
                                                    showToast(assmbleDetailBean.getMessage() + "");
                                                }
                                            }

                                            @Override
                                            public void onError(Response response) {
                                                super.onError(response);
                                                dismissDialog();
                                            }

                                        });
                            }
                        });
                    } else if (pItem.getStatus() == 4) {
                        right_tv.setVisibility(View.GONE);
                        center_tv.setVisibility(View.GONE);
                        left_tv.setVisibility(View.VISIBLE);
                        timeLl.setVisibility(View.GONE);
                        left_tv.setText("退款退货中");

                        left_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                    } else if (pItem.getStatus() == 6) {
                        right_tv.setVisibility(View.GONE);
                        left_tv.setVisibility(View.VISIBLE);
                        center_tv.setVisibility(View.GONE);
                        timeLl.setVisibility(View.GONE);
                        left_tv.setText("交易关闭");

                        left_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        });
                    }else if(pItem.getStatus() == 7){
                        right_tv.setText("查看详情");
                        left_tv.setText("查看物流");
                        right_tv.setVisibility(View.VISIBLE);
                        left_tv.setVisibility(View.VISIBLE);
                        center_tv.setVisibility(View.GONE);
                        timeLl.setVisibility(View.GONE);
                        left_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Bundle bundle = new Bundle();
                                bundle.putString("com", pItem.getExpressCompanyCode() + "");
                                bundle.putString("num", pItem.getExpressNumber() + "");
                                bundle.putString("state", pItem.getStatus() + "");
                                startActivity(LogisticsActivity.class, bundle);
                            }
                        });
                        right_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showDialog(AssmbeAllOrderFragment.this.context, null);
                                HashMap<String, Object> httpParams = new HashMap();
                                httpParams.put("orderId", pItem.getOrderId());
                                finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                                        Contacts.HOST_SERVER + Url.ASSMBLE_DETAIL_REFRESH,
                                        httpParams, Url.ASSMBLE_DETAIL_REFRESH, new Callback<AssmbleDetailBean>() {
                                            @Override
                                            public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                                                dismissDialog();
                                                if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                                                    Bundle bundle = new Bundle();
                                                    bundle.putSerializable("assmbleDetailBean", assmbleDetailBean);
                                                    startActivity(AssmbleOrderActivity.class, bundle);
                                                } else {
                                                    showToast(assmbleDetailBean.getMessage() + "");
                                                }
                                            }

                                            @Override
                                            public void onError(Response response) {
                                                super.onError(response);
                                                dismissDialog();
                                            }

                                        });
                            }
                        });
                    }
                } else if (pItem.getGroupStatus().equals("10")) {
                    assmble_status.setText("拼团失败");
                    right_tv.setVisibility(View.GONE);
                    center_tv.setVisibility(View.GONE);
                    left_tv.setVisibility(View.VISIBLE);
                    timeLl.setVisibility(View.GONE);
                    left_tv.setText("交易关闭");
                    left_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    });
                }

                XListView child_listview = helper.getView(R.id.child_listview);
                QuickAdapter<AssmbleOrderListBean.DataBean.PageInfoBean.ListBeanX.DetailListBean.ListBean> child_adapter =
                        new QuickAdapter<AssmbleOrderListBean.DataBean.PageInfoBean.ListBeanX.DetailListBean.ListBean>(context, R.layout.item_child_order_list) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, final AssmbleOrderListBean.DataBean.PageInfoBean.ListBeanX.DetailListBean.ListBean cItem1) {
                                helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Bundle bundle = new Bundle();
                                        bundle.putString("id", cItem1.getMerchandiseId());
                                        Logger.e(cItem1.getId());
                                        startActivity(AssmbleShopDetailActivity.class, bundle);
                                    }
                                });
                                helper.setText(R.id.title, cItem1.getMerchandiseName());
                                helper.setText(R.id.money, "￥" + cItem1.getSinglePrice());
                                List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(cItem1.getMcipValue(), ShopDetailMcipChildBean.class);
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int i = 0; i < mcipList.size(); i++) {
                                    if (i == mcipList.size() - 1) {
                                        stringBuilder.append(mcipList.get(i).getPropertyValue());
                                    } else {
                                        stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                                    }
                                }
                                helper.setText(R.id.num_spec, "数量：x" + cItem1.getPurchaseAmount() + ";" + stringBuilder.toString());
                                RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                                ImageView photo = helper.getView(R.id.photo);
                                Glide.with(AssmbeAllOrderFragment.this.context)
                                        .load(cItem1.getPhotoUrl())
                                        .error(R.mipmap.default_small_bg)
                                        .into(photo);
                                rc_layout.setTopLeftRadius(6);
                                rc_layout.setTopRightRadius(6);
                                rc_layout.setBottomLeftRadius(6);
                                rc_layout.setBottomRightRadius(6);

                                TextView return_money_goods = helper.getView(R.id.return_money_goods);
                                if (pItem.getGroupStatus().equals("20")) {
                                    if (pItem.getStatus() == 3) {
                                        return_money_goods.setVisibility(View.VISIBLE);
                                        return_money_goods.setText("退货/退款");

                                        return_money_goods.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Bundle bundle = new Bundle();
                                                bundle.putString("tag", "退货/退款");
                                                bundle.putString("orderId", pItem.getOrderId());
                                                bundle.putSerializable("DetailListBean", cItem1);
                                                startActivity(AssmbleRefundServiceActivity.class, bundle);
                                            }
                                        });
                                    } else if (pItem.getStatus() == 1) {
                                        return_money_goods.setVisibility(View.VISIBLE);
                                        return_money_goods.setText("退款");

                                        return_money_goods.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Bundle bundle = new Bundle();
                                                bundle.putString("tag", "退款");
                                                bundle.putString("orderId", pItem.getOrderId());
                                                bundle.putSerializable("DetailListBean", cItem1);
                                                startActivity(AssmbleRefundServiceActivity.class, bundle);
                                            }
                                        });
                                    } else {
                                        return_money_goods.setVisibility(View.GONE);
                                    }
                                } else {
                                    return_money_goods.setVisibility(View.GONE);
                                }

                            }
                        };
                child_listview.setAdapter(child_adapter);
                if (pItem.getDetailList() != null && pItem.getDetailList().size() != 0) {
                    child_adapter.clear();
                    child_adapter.addAll(pItem.getDetailList().get(0).getList());
                    child_adapter.notifyDataSetChanged();
                }
            }
        };

        listView.setAdapter(quickAdapter);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void OnMessageEvent(String s) {
        if (s.equals("")) {
            pageNum = 1;
            initData();
        } else if (s.equals("updateAssmbleOrderList")) {
            pageNum = 1;
            initData();
        }
    }

    @Override
    protected void initData() {
//        showDialog(context, null);
        HashMap<String, Object> httpParams = new HashMap();
        httpParams.put("status", status);
        httpParams.put("pageNum", pageNum++);
        httpParams.put("pageSize", pageSize);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT,
                Contacts.HOST_SERVER + Url.ASSMBLE_ORDER_LIST,
                httpParams, Url.ASSMBLE_ORDER_LIST, new Callback<AssmbleOrderListBean>() {
                    @Override
                    public void onSuccess(AssmbleOrderListBean orderListBean) {
                        if (orderListBean.getCode().equals(Constant.REQUEST_SUCCESS_0) && orderListBean.getData() != null && orderListBean.getData().getPageInfo().getList() != null && orderListBean.getData().getPageInfo().getList().size() != 0) {
                            refreshLayout.setNoMoreData(false);
                            if (pageNum - 1 == 1) {
                                quickAdapter.clear();
                                for (int i = 0; i < orderListBean.getData().getPageInfo().getList().size(); i++) {
                                    if (orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("10")
                                            || orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("20")
                                            || orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("30")
                                            || orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("31")) {
                                        if (orderListBean.getData().getPageInfo().getList().get(i).getCountDown() != null && !orderListBean.getData().getPageInfo().getList().get(i).getCountDown().contains("-")) {
                                            orderListBean.getData().getPageInfo().getList().get(i).setCountDown((Long.parseLong(orderListBean.getData().getPageInfo().getList().get(i).getCountDown()) * 1000 + System.currentTimeMillis()) + "");
                                        }
                                        quickAdapter.add(orderListBean.getData().getPageInfo().getList().get(i));
                                    }
                                }
                            } else {
                                for (int i = 0; i < orderListBean.getData().getPageInfo().getList().size(); i++) {
                                    if (orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("10")
                                            || orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("20")
                                            || orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("30")
                                            || orderListBean.getData().getPageInfo().getList().get(i).getGroupStatus().equals("31")) {
                                        if (orderListBean.getData().getPageInfo().getList().get(i).getCountDown() != null && !orderListBean.getData().getPageInfo().getList().get(i).getCountDown().contains("-")) {
                                            orderListBean.getData().getPageInfo().getList().get(i).setCountDown(Long.parseLong(orderListBean.getData().getPageInfo().getList().get(i).getCountDown()) * 1000 + System.currentTimeMillis() + "");
                                        }
                                        quickAdapter.add(orderListBean.getData().getPageInfo().getList().get(i));
                                    }
                                }
                            }
                            if (quickAdapter.getCount() > 0) {
                                empty.setVisibility(View.GONE);
                            } else {
                                empty.setVisibility(View.VISIBLE);
                            }
                        } else {
                            if (pageNum - 1 == 1) {
                                quickAdapter.clear();
                                empty.setVisibility(View.VISIBLE);
                            } else {
                                if (quickAdapter.getCount() > 0) {
                                    empty.setVisibility(View.GONE);
                                } else {
                                    empty.setVisibility(View.VISIBLE);
                                }
                            }
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        }

                        refreshLayout.finishRefresh();
                        refreshLayout.finishLoadMore();
                        quickAdapter.notifyDataSetChanged();
//                dismissDialog();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
//                dismissDialog();
                        try{
                            if (pageNum - 1 == 1) {
                                empty.setVisibility(View.VISIBLE);
                            } else {
                                empty.setVisibility(View.GONE);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                });
    }

    @Override
    @OnClick({R.id.empty})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.empty:
                onRefresh(refreshLayout);
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        initData();
    }

    @Override
    public void ZFBSuccess(String code) {
        dismissDialog();
        HashMap<String, Object> httpParams = new HashMap();
        httpParams.put("orderId", orderId);
        httpParams.put("payMode", Constant.NUM_1 + "");
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_ORDER_NOTIFY, httpParams, Url.ASSMBLE_ORDER_NOTIFY, new Callback<AssmbleDetailBean>() {
            @Override
            public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                    onRefresh(refreshLayout);
                    showPaySuccess();
                } else {
                    showFaileDialog();
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                showToast("网络异常");
                showFaileDialog();
            }
        });
    }

    @Override
    public void ZFBFail(String code) {
        dismissDialog();
        showFaileDialog();
    }

    @Override
    public void ZFBResultQueRen(String code) {

    }

    @Override
    public void callBack(BaseResp baseResp) {
        dismissDialog();
        if(baseResp.errCode != BaseResp.ErrCode.ERR_OK){
            return;
        }
        HashMap<String, Object> httpParams = new HashMap();
        httpParams.put("orderId", orderId);
        httpParams.put("payMode", Constant.NUM_2 + "");
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_ORDER_NOTIFY, httpParams, Url.ASSMBLE_ORDER_NOTIFY, new Callback<AssmbleDetailBean>() {
            @Override
            public void onSuccess(AssmbleDetailBean assmbleDetailBean) {
                if (assmbleDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)) {
                    onRefresh(refreshLayout);
                    showPaySuccess();
                } else {
                    showFaileDialog();
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                showToast("网络异常");
                showFaileDialog();
            }

        });
    }

    private void showPaySuccess() {
        DialogUtil.getInstance().showCenterDialog(context, R.layout.dialog_shop_pay_success);
        TextView message = DialogUtil.getInstance().dialog_center_layout.findViewById(R.id.message);
        message.setText("");
        message.setVisibility(View.INVISIBLE);
        DialogUtil.getInstance().dialog_center_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dialog_center.dismiss();
            }
        });
    }

    private void showFaileDialog() {
        LayoutInflater mInflater = LayoutInflater.from(context);
        LinearLayout dialog_pay_faile = (LinearLayout) mInflater.inflate(R.layout.dialog_pay_faile, null);
        Dialog dialog = new AlertDialog.Builder(context, R.style.NoBackGroundDialog).create();
        dialog.show();
        dialog.setContentView(dialog_pay_faile);
    }
}
